package com.ruoyi.web.easytron;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.web.easytron.BaseTest;
import com.ruoyi.web.easytron.model.Trc20ContractInfo;
import com.ruoyi.web.easytron.util.Trc20ContractUtil;
import com.ruoyi.web.easytron.util.TronConverter;
import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.tron.trident.core.ApiWrapper;
import org.tron.trident.core.contract.Contract;
import org.tron.trident.core.contract.Trc20Contract;
import org.tron.trident.core.exceptions.IllegalException;
import org.tron.trident.core.key.KeyPair;

import org.tron.trident.proto.Chain;
import org.tron.trident.proto.Response;
import org.tron.trident.utils.Convert;

public class TronUnit extends BaseTest {

    /**
     * # ApiWrapper原装 - 获取trc20余额

     public static void simple_balanceOfTrc20() {
     // 查询的地址
     String address = "TNXTWPUQKaTfLY2zaBMwHm6mt2z3pJbjSp";
     // 获取合约信息
     Contract contract = wrapper.getContract(contractAddress);
     // 构造trc20合约信息
     Trc20Contract trc20Contract = new Trc20Contract(contract, address, wrapper);
     // 合约精度
     BigInteger decimals = trc20Contract.decimals();
     // 余额
     BigInteger balance = trc20Contract.balanceOf(address);
     // 真实余额 单位 个
     BigDecimal result = TronConverter.getRealAmount(new BigDecimal(balance), decimals.intValue());

     System.out.println("------------剩余数量："+result+"个");
     }

     /**
     * 获取trc20余额

     public static void balanceOfTrc20() {
     // 地址
     String address = "TNXTWPUQKaTfLY2zaBMwHm6mt2z3pJbjSp";
     BigDecimal transferAmount = Trc20ContractUtil.trc20BalanceOf(contractAddress, address, wrapper);

     System.out.println("------------Trc20余额："+transferAmount+"个");
     // 合约信息
     Trc20ContractInfo trc20ContractInfo = Trc20ContractUtil.readTrc20ContractInfo(contractAddress, wrapper);
     BigDecimal realAmount = trc20ContractInfo.getRealAmount(transferAmount).stripTrailingZeros();

     System.out.println("------------Trc20真实余额："+realAmount+"个");
     }
     */
    /**
     * # ApiWrapper原装 - 获取trx余额
     */
    public static void simple_balanceOfTrx() {
        // 地址
        // 获取账户信息
        Response.Account account = wrapper.getAccount(from);
        // 余额
        long balance = account.getBalance();
        // 真实余额
        BigDecimal trx = Convert.fromSun(new BigDecimal(balance), Convert.Unit.TRX);

        System.out.println("------------trx余额："+trx.toString()+"个");

        long sum = account.getFrozenList().stream().mapToLong(Response.Account.Frozen::getFrozenBalance).sum();
        long frozenBalance = account.getAccountResource().getFrozenBalanceForEnergy().getFrozenBalance();
        long frozen = sum + frozenBalance;
        BigDecimal frozenTrx = Convert.fromSun(new BigDecimal(frozen), Convert.Unit.TRX);

        System.out.println("------------trx质押："+frozenTrx+"个");
        System.out.println("------------trx 总余额："+trx.add(frozenTrx)+"个");
    }

    /**
     * trx 转账 - trident原生 - 非本地构造交易
     */
    @Test
    public static void transferTrx_trident() throws IllegalException {

        // trx 个数
        BigDecimal realAmount = BigDecimal.valueOf(0);
        // sun 个数
        BigDecimal sun = Convert.toSun(realAmount, Convert.Unit.TRX);

        // 远程构造交易
        Response.TransactionExtention transfer =
                wrapper.transfer(from, to, sun.longValue());
        // 签名
        Chain.Transaction signTransaction = wrapper.signTransaction(transfer);
        // 广播
        String tid = wrapper.broadcastTransaction(signTransaction);
        System.out.println("------------广播："+tid);
    }
    public  static  void  getYue() {
        String privateKey = "6b898e92660728467e9bd3117f3b37ed026e607032570d0e2ec63de2f2854e09";
        String baseAddress = "TNXTWPUQKaTfLY2zaBMwHm6mt2z3pJbjSp";


        BigDecimal divsisor=new BigDecimal(1000000);
        // // 测试环境不需要apike
        ApiWrapper client=new ApiWrapper("grpc.nile.trongrid.io:50051", "grpc.nile.trongrid.io:50061", privateKey);



        System.out.println(">>>>>>>余额:"+divsisor.toPlainString());

    }

    /**
     * 离线生成地址，公钥，私钥
     * */
    public  static  void  setAddressLX(int ports)
    {
        KeyPair keyPair = KeyPair.generate();
        String privateKey = keyPair.toPrivateKey();
        String base58CheckAddress = keyPair.toBase58CheckAddress();
        String publicKey = keyPair.toPublicKey();
        Map<String, String> stringMap = new HashMap<>();
        stringMap.put("address", base58CheckAddress);
        System.out.println(base58CheckAddress);
        stringMap.put("privateKey", privateKey);
        System.out.println(privateKey);
        stringMap.put("publicKey", publicKey);
        System.out.println(publicKey);
    }
    //Tron波场区块链 | 使用Java将Tron钱包助记词转私钥 全网独门一份
//https://blog.csdn.net/t610654893/article/details/137183319?ops_request_misc=&request_id=&biz_id=102&utm_term=java%20%20Tron&utm_medium=distribute.pc_search_result.none-task-blog-2~all~sobaiduweb~default-4-137183319.142^v100^pc_search_result_base2&spm=1018.2226.3001.4449
}
